<template>
  <div class="container">
    <h3>{{ text }}</h3>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="age">
        <el-input v-model="ruleForm.age"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input
          type="password"
          v-model="ruleForm.pass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass" v-if="checkpwd">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          v-for="(item, index) in btn"
          :type="item['type']"
          @click="submitForm('ruleForm', item['type'])"
        >
          {{ item["text"] }}
        </el-button>
      </el-form-item>
    </el-form>
    <div class="button">
      <a href="" @click="changeUrl()" v-if="reg">还未注册?点此注册</a>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    reg: Boolean,
    isChecked: Boolean,
    text: String,
    checkpwd: String,
  },
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      checked: "",
      btn: [
        { text: "提交", type: "primary", types: "btn" },
        { text: "重置", type: "", types: "reset" },
      ],

      ruleForm: {
        pass: "",
        checkPass: "",
        age: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        age: [{ validator: checkAge, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName, type) {
      if (type === "") {
        this.$refs[formName].resetFields();
      }
      if (this.$route.path === "/regist") {
        this.$router.push("/login");
      } else if (this.$route.path === "/login") {
        this.$router.push("/index");
      }
      // this.$refs[formName].validate((valid) => {
      //   if (valid) {
      //     alert('submit!');
      //   } else {
      //     console.log('error submit!!');
      //     return false;
      //   }
      // });
    },
    changeUrl() {
      this.$router.push("/regist");
    },
  },
};
</script>

<style scoped lang="less">
.container {
  height: 60%;
  width: 35%;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background-color: rgba(255, 255, 255, 0.7);
  border-radius: 3%;

  h3 {
    text-align: center;
    margin: 0.8rem 0;
    font-size: 1.25rem;
  }

  .el-form {
    width: 90%;
    margin: 0 auto;
  }

  .button {
    padding-left: 1.5rem;

    a {
      color: #42b983;

      &:hover {
        color: #ad2c57;
        text-decoration: none;
      }
    }
  }
}
</style>
